package com.ruoyi.common.sdk.sms.api.impl;

import com.ruoyi.common.sdk.sms.api.ISmsGateWayApi;
import com.ruoyi.common.sdk.sms.vo.request.SendMsgRequest;
import com.ruoyi.common.sdk.sms.vo.request.SendValidMsgRequest;
import com.ruoyi.common.sdk.sms.vo.request.ValidYzmRequest;
import com.ruoyi.common.sdk.sms.vo.respone.SendMsgRespone;
import com.ruoyi.common.sdk.sms.vo.respone.ValidYzmRespone;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;

@Service
public class SmsGateWayApi implements ISmsGateWayApi {

    @Autowired
    RestTemplate restTemplate;

    private static final String SMSGATEWAY_SENDMSG = "http://180.101.182.191:9840/prod-api/sms/smsApi/sendSms";
    private static final String SMSGATEWAY_SENDVALIDMSG = "http://180.101.182.191:9840/prod-api/sms/smsApi/sendValid";
    private static final String SMSGATEWAY_VALIDYZM = "http://180.101.182.191:9840/prod-api/sms/smsApi/valid";

    /**
     * 发送短信
     */
    @Override
    public SendMsgRespone sendMsg(SendMsgRequest request) throws UnsupportedEncodingException {
        /** 判断签名是否生成 */
        if(StringUtils.isEmpty(request.getSign())) request.createSign();

        return restTemplate.postForObject(SMSGATEWAY_SENDMSG, request, SendMsgRespone.class);
    }

    /**
     * 发送验证码短信
     */
    @Override
    public SendMsgRespone sendValidMsg(SendValidMsgRequest request) throws UnsupportedEncodingException {
        /** 判断签名是否生成 */
        if(StringUtils.isEmpty(request.getSign())) request.createSign();

        return restTemplate.postForObject(SMSGATEWAY_SENDVALIDMSG, request, SendMsgRespone.class);
    }

    /**
     * 短信验证码 校验
     */
    @Override
    public ValidYzmRespone validYzm(ValidYzmRequest request) throws UnsupportedEncodingException {
        /** 判断签名是否生成 */
        if(StringUtils.isEmpty(request.getSign())) request.createSign();

        return restTemplate.postForObject(SMSGATEWAY_VALIDYZM, request, ValidYzmRespone.class);
    }
}
